Crate decompress

source ·
Expand description

Decompress an archive, supporting multiple format and stripping path prefixes

Usage

You can use the default decompress stack:

#![allow(clippy::cognitive_complexity)]

use clap::{arg, command};
use decompress::ExtractOptsBuilder;

fn main() {
    let matches = command!()
        .arg(arg!(<archive> "Archive to extract"))
        .arg(arg!(<out> "Output folder"))
        .arg(arg!(
            -s --strip "Strip the first component of the archive"
        ))
        .get_matches();

    let archive = matches.get_one::<String>("archive").expect("required");
    let to = matches.get_one::<String>("out").expect("required");
    let strip = usize::from(matches.get_flag("strip"));
    let res = decompress::decompress(
        archive,
        to,
        &ExtractOptsBuilder::default().strip(strip).build().unwrap(),
    );
    println!("{res:?}");
}

Or build your own stack:

#![allow(clippy::cognitive_complexity)]

use clap::{arg, command};
use decompress::{decompressors, ExtractOptsBuilder};
use regex::Regex;

fn main() {
    let matches = command!()
        .arg(arg!(<archive> "Archive to Unzip (attempt any file)"))
        .arg(arg!(<out> "Output folder"))
        .arg(arg!(
            -s --strip "Strip the first component of the archive"
        ))
        .get_matches();

    let archive = matches.get_one::<String>("archive").expect("required");
    let to = matches.get_one::<String>("out").expect("required");
    let strip = usize::from(matches.get_flag("strip"));
    let decompressor = decompress::Decompress::build(vec![decompressors::zip::Zip::build(Some(
        Regex::new(r".*").unwrap(),
    ))]);

    let res = decompressor.decompress(
        archive,
        to,
        &ExtractOptsBuilder::default()
            .strip(strip)
            .filter(|path| {
                if let Some(path) = path.to_str() {
                    return path.ends_with("ex.sh");
                }
                false
            })
            .build()
            .unwrap(),
    );

    println!("{res:?}");
}

NOTE: to include or exclude decompressors types, use --features and disable default. This in turn removes or includes the (costly) dependencies these features need.

Modules

Structs

Enums

Traits

  • Decompressor is a trait that you can implement to add your own decompressor type. A Decompressor is inserted into a stack, where given a potential archive file, many decompressors may attempt to test if they’re capable of unpacking it. The first Decompressor which will test true will be the one selected to unpack.

Functions

  • Returns true if any of the decompressors in the stack can decompress this specific archive based on its path (no file opening)
  • Returns true if any of the decompressors in the stack can decompress this specific archive based on its content (reads first 8kb)
  • Decompress an archive with default decompressor set up
  • List an archive with default decompressor set up

Type Definitions